/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ordering.dao;

import com.ordering.dao.exceptions.NonexistentEntityException;
import com.ordering.pojo.UserManage;
import com.ordering.utils.ConfigIni;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;

/**
 *
 * @author hua
 */
public class UserManageJpaController {

    public UserManageJpaController() {
        emf = EntityManagerUtils.em;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(UserManage userManage) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(userManage);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(UserManage userManage) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            userManage = em.merge(userManage);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = userManage.getPk();
                if (findUserManage(id) == null) {
                    throw new NonexistentEntityException("The userManage with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            UserManage userManage;
            try {
                userManage = em.getReference(UserManage.class, id);
                userManage.getPk();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The userManage with id " + id + " no longer exists.", enfe);
            }
            em.remove(userManage);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<UserManage> findUserManageEntities() {
        return findUserManageEntities(true, -1, -1);
    }

    public List<UserManage> findUserManageEntities(int maxResults, int firstResult) {
        return findUserManageEntities(false, maxResults, firstResult);
    }

    private List<UserManage> findUserManageEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from UserManage as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public UserManage findUserManage(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(UserManage.class, id);
        } finally {
            em.close();
        }
    }

    public int getUserManageCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from UserManage as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

    public UserManage findUserManageByUserId(String userId)
    {
         EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from UserManage as o where o.userid=?");
            q.setParameter(1, userId);

              List<UserManage> temp = q.getResultList();
              if (temp == null || temp.isEmpty())
              {
                  return null;
              }
                return temp.iterator().next();
        } finally {
            em.close();
        }
    }

    void destroy(UserManage userManage) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

}
